const path = require("path");
const { execQueryUrl } = require("./utils");
module.exports = class VuePlugin {
  apply(compiler) {
    const rules = compiler.options.module.rules;
    const pitcher = {
      loader: path.resolve(__dirname, "./pitcher"),
      resourceQuery(query) {
        const param = execQueryUrl(query);
        return param.has("vue");
      },
    };

    const cloneRules = rules.map((rule) => {
      const cloneRule = {
        ...rule,
      };
      cloneRule.resourceQuery = function (query) {
        const param = execQueryUrl(query);
        return param.has("vue") && rule.test.test("." + param.get("lang"));
      };
      delete cloneRule.test;
      return cloneRule;
    });

    compiler.options.module.rules = [pitcher, ...cloneRules, ...rules];
  }
};
